package com.shuosen.gmall.order.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.shuosen.gmall.bean.*;
import com.shuosen.gmall.bean.enums.OrderStatus;
import com.shuosen.gmall.bean.enums.ProcessStatus;
import com.shuosen.gmall.config.LoginRequire;
import com.shuosen.gmall.service.CartService;
import com.shuosen.gmall.service.OrderService;
import com.shuosen.gmall.service.Userservice;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
public class OrderController {


    @Reference
    private  CartService cartService  ;
    /***
     * 要使用dubbo的reference来调用服务
     */
    @Reference
    private Userservice userservice ;

    @Reference
    private OrderService orderService  ;

    @RequestMapping("trade")
    @LoginRequire
    public String tradeInit(HttpServletRequest request ){
        String userId = (String) request.getAttribute("userId");
        System.out.println("消费端-----------------------");
        //调用服务查询用户地址列表
        List<UserAddress> userAddressList = userservice.findAdressByUserId(userId);

        //获取被选中的商品

        List<OrderDetail> orderDetailList = new ArrayList<>();
        List<CartInfo> cartInfoList = cartService.getCartCheckedList(userId);
        for (CartInfo cartInfo : cartInfoList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setSkuId(cartInfo.getSkuId());
            orderDetail.setSkuName(cartInfo.getSkuName());
            orderDetail.setImgUrl(cartInfo.getImgUrl());
            orderDetail.setOrderPrice(cartInfo.getCartPrice());
            orderDetail.setSkuNum(cartInfo.getSkuNum());
            orderDetailList.add(orderDetail);
        }

        OrderInfo orderInfo  = new OrderInfo()  ;
        //orderDetailList放到orderInfo中
        orderInfo.setOrderDetailList(orderDetailList);
        orderInfo.sumTotalAmount();
        //保存到作用域
        request.setAttribute("totalAmount",orderInfo.getTotalAmount());
        request.setAttribute("orderDetailList",orderDetailList);
        request.setAttribute("userAddressList",userAddressList);
        return "trade";
    }

    @RequestMapping("submitOrder")
    @LoginRequire
    public String submitOrder(HttpServletRequest request  ,OrderInfo  orderInfo ){

        String userId = (String) request.getAttribute("userId");

        //还差总进程状态  useId 总金额 订单的状态 都没有  
        orderInfo.setProcessStatus(ProcessStatus.UNPAID);
        orderInfo.setOrderStatus(OrderStatus.UNPAID);
        //计算 
        orderInfo.sumTotalAmount();
        orderInfo.setUserId(userId);

        String orderId = orderService.saveOrder(orderInfo);

        //调用服务层
        return "redirect://payment.gmall.com/index?orderId="+orderId;
    }


    //1:库存系统：  http://www.gware.com/index.html
    //
    @RequestMapping("orderSplit")
    @ResponseBody
    public String orderSplit(HttpServletRequest request){
        String orderId = request.getParameter("orderId");
        String wareSkuMap = request.getParameter("wareSkuMap");

        //如果需要拆单，获取子订单的集合
        List<OrderInfo> subOrderInfoList = orderService.orderSplit(orderId,wareSkuMap);
        //将子订单循环 变成我们想要的数据格式
        ArrayList<Map> mapList = new ArrayList<>();
        if(subOrderInfoList!=null && subOrderInfoList.size()>0){
            for (OrderInfo orderInfo : subOrderInfoList) {
                Map map = orderService.initWareOrder(orderInfo);
                mapList.add(map);
            }
        }
        return JSON.toJSONString(mapList);
    }


}
